﻿using Models.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Filters;

namespace Web.Controllers.MVC
{
    [MvcAuthentication]
    public class GoodsStockController : Controller
    {
        // GET: GoodsStock
        public ActionResult GoodsStockList()
        {

            DataTable udt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BillName").Tables[0];
            string s = "";
            foreach (DataRow item in udt.Rows)
            {
                s = s + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.BillName = s;


            return View();
        }
        [HttpPost]
        public string GoodsStockListAsync(int PageIndex = 1, int PageSize = 1000000000, string Search = "",int BillNameID =0)
        {
            GridResult result = new GridResult();
            SqlParameter[] parameters = {
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@BillNameID",SqlDbType.Int),
                                            new SqlParameter("@Search",SqlDbType.NVarChar,40000),
            };
            parameters[0].Value = PageIndex;
            parameters[1].Value = PageSize;
            parameters[2].Value = BillNameID;
            parameters[3].Value = Search;
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryGoodsStockList", parameters, "aaa", 3000);
            result.count = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]);
            result.data = ds.Tables[1];
            result.totalRow = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[2]))[0];
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }
        public ActionResult GoodsStockRunningsList(int BillNameID,int GoodsID)
        {
            ViewBag.BillNameID = BillNameID;
            ViewBag.GoodsID = GoodsID;
            return View();
        }
        [HttpPost]
        public string GoodsStockRunningsListAsync(int BillNameID, int GoodsID,int PageIndex = 1, int PageSize = 1000000000)
        {
            GridResult result = new GridResult();
            SqlParameter[] parameters = {
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@BillNameID",SqlDbType.Int),
                                            new SqlParameter("@GoodsID",SqlDbType.Int),
            };
            parameters[0].Value = PageIndex;
            parameters[1].Value = PageSize;
            parameters[2].Value = BillNameID;
            parameters[3].Value = GoodsID;
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryGoodsStockRunningsList", parameters, "aaa", 3000);
            result.count = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]);
            result.data = ds.Tables[1];
            result.totalRow = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[2]))[0];
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }

        public ActionResult MoveBillList()
        {

            DataTable udt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BillName").Tables[0];
            string s = "";
            foreach (DataRow item in udt.Rows)
            {
                s = s + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.BillName = s;


            return View();
        }
        [HttpPost]
        public string MoveBillListAsync(int PageIndex = 1, int PageSize = 1000000000, int GoodsID = 0, int BillNameID = 0)
        {
            GridResult result = new GridResult();
            SqlParameter[] parameters = {
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@BillNameID",SqlDbType.Int),
                                            new SqlParameter("@GoodsID",SqlDbType.Int),
                                            new SqlParameter("@UserID",SqlDbType.Int),
            };
            parameters[0].Value = PageIndex;
            parameters[1].Value = PageSize;
            parameters[2].Value = BillNameID;
            parameters[3].Value = GoodsID;
            parameters[4].Value = Common.CookiesManagement.GetUserModel().UserID;
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryMoveBillList", parameters, "aaa", 3000);
            result.count = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]);
            result.data = ds.Tables[1];
            result.totalRow = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[2]))[0];
            return Newtonsoft.Json.JsonConvert.SerializeObject(result);
        }

        public ActionResult MoveBillEdit(int ID = 0)
        {
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select MoveBill.*,isnull(Goods.Code,'') as GoodsCode,isnull(Goods.Name,'') as GoodsName,isnull(Goods.Model,'') as GoodsModel
		,inb.Name as InBillName,outb.Name as OutBillName
FROM MoveBill inner join Goods on MoveBill.GoodsID=Goods.ID 
	inner join BillName as inb on MoveBill.InBillNameID=inb.ID
	inner join BillName as outb on MoveBill.OutBillNameID=outb.ID
WHERE MoveBill.ID=" + ID).Tables[0];
            string sssss = Newtonsoft.Json.JsonConvert.SerializeObject(dt);
            Models.MVC.MoveBillMode m = new Models.MVC.MoveBillMode();
            if (dt.Rows.Count > 0)
            {
                m = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.MVC.MoveBillMode>>(Newtonsoft.Json.JsonConvert.SerializeObject(dt))[0];
            }
            ViewBag.model = m;

            DataTable udt = FreeADO.DBUtility.DbHelperSQL.Query("select * from Users where IsDeleted=0").Tables[0];
            string s = "";
            foreach (DataRow item in udt.Rows)
            {
                s = s + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.Users = s;

            string IDs = "0";
            if (Web.Common.Common.IsAuthoritys(56) > 0)
            {
                IDs = IDs + ",1";
            }
            if (Web.Common.Common.IsAuthoritys(57) > 0)
            {
                IDs = IDs + ",2";
            }
            if (Web.Common.Common.IsAuthoritys(58) > 0)
            {
                IDs = IDs + ",3";
            }
            if (Web.Common.Common.IsAuthoritys(59) > 0)
            {
                IDs = IDs + ",4";
            }
            if (Web.Common.Common.IsAuthoritys(60) > 0)
            {
                IDs = IDs + ",5";
            }
            if (Web.Common.Common.IsAuthoritys(61) > 0)
            {
                IDs = IDs + ",6";
            }
            if (Web.Common.Common.IsAuthoritys(62) > 0)
            {
                IDs = IDs + ",7";
            }
            if (Web.Common.Common.IsAuthoritys(103) > 0)
            {
                IDs = IDs + ",8";
            }
            if (Web.Common.Common.IsAuthoritys(104) > 0)
            {
                IDs = IDs + ",9";
            }
            DataTable bdt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BillName  where ID in(" + IDs + ")").Tables[0];
            string bs = "";
            foreach (DataRow item in bdt.Rows)
            {
                bs = bs + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.BillNameOut = bs;
            DataTable bdti = FreeADO.DBUtility.DbHelperSQL.Query("select * from BillName").Tables[0];
            string bsi = "";
            foreach (DataRow item in bdti.Rows)
            {
                bsi = bsi + @"<option value=""" + item["ID"].ToString() + @""">" + item["Name"].ToString() + @"</option>";
            }
            ViewBag.BillNameIn = bsi;


            return View();
        }
        [HttpPost]
        public ActionResult MoveBillEditAsync(Models.MVC.MoveBillMode m)
        {
            Result<decimal> result = new Result<decimal>() { Type = AjaxResultType.Success, Content = "成功", Data = 0, TotalRows = 0 };
            if (m.Remark==null)
            {
                m.Remark = "";
            }
            try
            {
                SqlParameter[] paras ={
                    new SqlParameter("@ID",SqlDbType.Int),
                    new SqlParameter("@InBillNameID",SqlDbType.Int),
                    new SqlParameter("@OutBillNameID",SqlDbType.Int),
                    new SqlParameter("@InUserID",SqlDbType.Int),
                    new SqlParameter("@OutUserID",SqlDbType.Int),
                    new SqlParameter("@GoodsID",SqlDbType.Int),
                    new SqlParameter("@Qty",SqlDbType.Int),
                    new SqlParameter("@WaiXing",SqlDbType.NVarChar,4000),
                    new SqlParameter("@BillCode",SqlDbType.NVarChar,4000),
                    new SqlParameter("@Remark",SqlDbType.NVarChar,4000),
                    new SqlParameter("@Str",SqlDbType.NVarChar,4000),
                    new SqlParameter("@ReturnValue", SqlDbType.Int)
                                       };
                paras[0].Value = m.ID;
                paras[1].Value = m.InBillNameID;
                paras[2].Value = m.OutBillNameID;
                paras[3].Value = m.InUserID;
                paras[4].Value = m.OutUserID;
                paras[5].Value = m.GoodsID;
                paras[6].Value = m.Qty;
                paras[7].Value = m.WaiXing;
                paras[8].Value = m.BillCode;
                paras[9].Value = m.Remark;
                paras[10].Value = "";
                paras[10].Direction = ParameterDirection.InputOutput;
                paras[11].Direction = ParameterDirection.ReturnValue;

                DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_MoveBillEdit", paras, "aaa", 3000);
                if (Convert.ToInt32(paras[11].Value) == 1)
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = 0;
                }
                else
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = paras[10].Value.ToString();
                    result.Data = 0;
                }
            }
            catch (Exception e)
            {
                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = 0;
            }
            return Json(result);
        }
        public ActionResult OtherStockEdit(int GoodsID = 0,int BillNameID=0,int Type=0)
        {
            ViewBag.GoodsID = GoodsID;
            ViewBag.BillNameID = BillNameID;
            ViewBag.Type = Type;
            if (Type==2)
            {
                ViewBag.Title = "其它入库";
            }
            else
            {
                ViewBag.Title = "其它出库";
            }


            return View();
        }
        [HttpPost]
        public ActionResult OtherStockEditAsync(int ID=0,int BillNameID=0,int GoodsID=0,int Type=0,int Qty=0,string Remark="")
        {
            Result<decimal> result = new Result<decimal>() { Type = AjaxResultType.Success, Content = "成功", Data = 0, TotalRows = 0 };
            if (Remark == null)
            {
                Remark = "";
            }
            try
            {
                SqlParameter[] paras ={
                    new SqlParameter("@ID",SqlDbType.Int),
                    new SqlParameter("@BillNameID",SqlDbType.Int),
                    new SqlParameter("@GoodsID",SqlDbType.Int),
                    new SqlParameter("@Type",SqlDbType.Int),
                    new SqlParameter("@Qty",SqlDbType.Int),
                    new SqlParameter("@Remark",SqlDbType.NVarChar,4000),
                    new SqlParameter("@Str",SqlDbType.NVarChar,4000),
                    new SqlParameter("@ReturnValue", SqlDbType.Int)
                                       };
                paras[0].Value = ID;
                paras[1].Value = BillNameID;
                paras[2].Value = GoodsID;
                paras[3].Value = Type;
                paras[4].Value = Qty;
                paras[5].Value = Remark;
                paras[6].Value = "";
                paras[6].Direction = ParameterDirection.InputOutput;
                paras[7].Direction = ParameterDirection.ReturnValue;

                DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_OtherStockEdit", paras, "aaa", 3000);
                if (Convert.ToInt32(paras[7].Value) == 1)
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = 0;
                }
                else
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = paras[6].Value.ToString();
                    result.Data = 0;
                }
            }
            catch (Exception e)
            {
                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = 0;
            }
            return Json(result);
        }

    }
}